package com.news.by.news;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.news.by.R;

public class by extends Activity {
    /** Called when the activity is first created. */
	
	private WebView wv;
	private String LASTURL = "";
	private Menu myMenu;
	private SharedPreferences sp;
    String LIST_PREF_KEY = "listPref";
    Activity activity;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        
        sp = PreferenceManager.getDefaultSharedPreferences(this);

        wv = (WebView)findViewById(R.id.wv);
        WebSettings webSetting = wv.getSettings();
        webSetting.setJavaScriptEnabled(true);

        activity = this;        

        wv.setWebChromeClient(new WebChromeClient(){
        		public void onProgressChanged(WebView view, int progress){
        			activity.setTitle(" " + LASTURL);
        			activity.setProgress(progress * 100);        			
        			
        			if(progress == 100){
        				activity.setTitle(" " + LASTURL);
        			}
        		}
        	});
        
        wv.setWebViewClient(new WebViewClient(){
        	public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
        		Toast.makeText(getApplicationContext(), "Ошибка: " + description+ " " + failingUrl, Toast.LENGTH_LONG).show();
        	}
        	
        	public void onPageStarted(WebView view, String url, Bitmap favicon){
        		LASTURL = url;
        		view.getSettings().setLoadsImagesAutomatically(false);
        	}
        	
        	public void onPageFinished(WebView view, String url){
        		String jscontent = "";
        		
        		try{        			
        			InputStream is = getAssets().open("tut.by.js");
        	        InputStreamReader isr = new InputStreamReader(is);
        	        BufferedReader br = new BufferedReader(isr);

        	        String line;
        	        while (( line = br.readLine()) != null) {
        	        	jscontent += line;
        	        }
        	        is.close(); 
        	    }
        	    catch(Exception e){Toast.makeText(getApplicationContext(), "Ошибка: " + e.getMessage(), Toast.LENGTH_LONG).show();}
        	    
        		view.loadUrl("javascript:(function() { " + jscontent + "})()");
        		view.getSettings().setLoadsImagesAutomatically(true);        		
        	}
        });
        if (sp.getString(LIST_PREF_KEY,"tut.by").equalsIgnoreCase("tut.by")){
        	wv.loadUrl("http://"+sp.getString(LIST_PREF_KEY,"tut.by"));
        }
        else{}
    }
    
 	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
      if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
        wv.goBack();
        return true;
      }
      return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
      super.onCreateOptionsMenu(menu);
      
      this.myMenu = menu;
      myMenu.add(0, 1, 0, "Настройки");      
      //item.setIcon(R.drawable.icon);           
      myMenu.add(0, 2, 0, "Погода");      
      return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)  {
      switch (item.getItemId())
      {
        case 1:
        	Intent intent = new Intent(this, Prefs.class);        	
        	startActivity(intent);
          break;
          
        case 2:
        	intent = new Intent(this, Weather.class);        	
        	startActivity(intent);        	
        	break;
      }
      return true;
    }

    @Override
	public void onResume() {
		super.onResume();		
		if ((LASTURL.indexOf(sp.getString(LIST_PREF_KEY, "-1")) == -1) && LASTURL != ""){
			wv.loadUrl("http://"+sp.getString(LIST_PREF_KEY, "-1"));
		}		
	}    
}